<?php

namespace app\model;

//use support\Model;
use app\controller\IndexController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaOrdersQuotationRelevance
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_orders_quotation_relevance';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $list = Db::table('wa_orders_quotation_relevance')
            ->select('*')
            ->where($condition)
            ->get();
        if ($list){
            foreach ($list as $k){

                $k->price_options = json_decode($k->price_options);
                $k->option_package = json_decode($k->option_package);
                if ($k->other_price){
                    $k->other_price = json_decode($k->other_price);
                }
                // car_type  1 轿车 2 SUV 3 MPV 4 跑车 5 新能源
                if ($k->car_type == 1){
                    $k->car_type_name = '轿车';
                }elseif($k->car_type == 2){
                    $k->car_type_name = 'SUV';
                }elseif($k->car_type == 3){
                    $k->car_type_name = 'MPV';
                }elseif($k->car_type == 4){
                    $k->car_type_name = '跑车';
                }elseif($k->car_type == 5){
                    $k->car_type_name = '新能源';
                }else{
                    $k->car_type_name = '';
                }

                // 是否新能源 1 是 2 传统能源
                if ($k->is_nev == 1){
                    $k->is_nev_name = '新能源';
                }elseif ($k->is_nev == 2){
                    $k->is_nev_name = '传统能源';
                }else{
                    $k->is_nev_name = '';
                }

                // 车辆状况(是否是新车)  1 新车 2 二手车
                if ($k->vehicle_status == 1){
                    $k->vehicle_status_name = '新车';
                }elseif ($k->vehicle_status == 2){
                    $k->vehicle_status_name = '二手车';
                }else{
                    $k->vehicle_status_name = '';
                }


                //  1 预售待采购 2 库存待销售 3部分预售单（两者都有）
                if ($k->product_type == 1){
                    $k->product_type_name = '预售待采购';
                }elseif ($k->product_type == 2){
                    $k->product_type_name = '库存待销售';
                }elseif ($k->product_type == 3){
                    $k->product_type_name = '部分预售单';
                }else{
                    $k->product_type_name = '';
                }

                // 上牌服务商
                if ($k->registration_id){
                   $registration_fee =  WaSupplierRegistrationFee::find(['id' => intval($k->registration_id)]);
                    $registration_fee = json_decode($registration_fee);
                    if ($registration_fee){
                        $k->registration_name = $registration_fee->name;
                    }else{
                        $k->registration_name = '';
                    }

                }else{
                    $k->registration_name = '';
                }

                // 转移待出口服务商
                if ($k->transfer_export_id){
                    $transfer_export_fee =  WaSupplierRegistrationFee::find(['id' => intval($k->transfer_export_id)]);
                    $transfer_export_fee = json_decode($transfer_export_fee);
                    if ($transfer_export_fee){
                        $k->transfer_export_name = $transfer_export_fee->name;
                    }else{
                        $k->transfer_export_name = '';
                    }

                }else{
                    $k->transfer_export_name = '';
                }

            }
        }

        return json_encode($list);
    }


    public static function allByPurchaseQuotation($field,$condition)
    {
        $list = Db::table('wa_orders_quotation_relevance')
            ->select($field)
            ->where($condition)
            ->orderBy('purchase_quotation','ASC')
            ->get();
        return json_encode($list);
    }


    public static function ordersCount($start_time,$end_time)
    {

        $start_time = date('Y-m-d H:i:s', $start_time);
        $end_time = date('Y-m-d H:i:s', $end_time);
        $provider_count =Db::table('wa_orders')
            ->select('*')
            ->where('post_type','wa_orders_quotation_relevance')
            ->whereBetween('created_at',[$start_time,$end_time])
            ->count(); // 用户数量
        return $provider_count;

    }

    public static function created($data){
        $result = Db::table('wa_orders_quotation_relevance')->insertGetId($data);
        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_orders_quotation_relevance')->where($where)->update($data);
        return $result;
    }

    public static function geted($where){
        $result = Db::table('wa_orders_quotation_relevance')->where($where)
            ->get();
        return json_decode($result);
    }

    public static function delete($where){
        $result = Db::table('wa_orders_quotation_relevance')->where($where)->delete();
        return $result;
    }

    public static function sums($where){
        $result = Db::table('wa_orders_quotation_relevance')->where($where)->sum('total_price');
        return $result;
    }

    public static function counts($where){
        $result = Db::table('wa_orders_quotation_relevance')->where($where)->sum('count');
        return $result;
    }


}